package com.slipper.system.employee.controller;

import java.util.*;
import java.util.stream.Collectors;

import com.slipper.constants.Constants;
import com.slipper.system.menu.bo.MenuBO;
import com.slipper.system.role.bo.RoleBO;
import com.slipper.system.role.service.RoleService;
import jakarta.annotation.Resource;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.slipper.system.employee.bo.EmployeeBO;
import com.slipper.system.employee.service.EmployeeService;
import com.slipper.util.ResultUtil;

/**
 * 员工信息表
 */
@RestController
@RequestMapping("/system/employee")
public class EmployeeController {
    @Resource
    private EmployeeService employeeService;
    @Resource
    private RoleService roleService;

    @PostMapping("/queryBy")
    public String queryBy(@RequestBody EmployeeBO bo) {
        return ResultUtil.successResult(employeeService.queryBy(bo));
    }

    @PostMapping("/queryByIds")
    public String queryByIds(@RequestBody EmployeeBO bo) {
        List<EmployeeBO> employeeList = employeeService.queryByIds(bo);
        StringBuilder names = new StringBuilder();
        for (String id : bo.getIds()) {
            String name = "";
            for (EmployeeBO employeeBO : employeeList) {
                if (id.equals(employeeBO.getId())) {
                    name = employeeBO.getName();
                    break;
                }
            }
            if (names.isEmpty()) {
                names.append(name);
            } else {
                names.append(",").append(name);
            }
        }
        return ResultUtil.successResult(names.toString());
    }

    @PostMapping(value = "/queryList")
    public String queryList(@RequestBody EmployeeBO bo) {
        return ResultUtil.successResult(employeeService.queryList(bo));
    }


    @PostMapping("/queryPage")
    public String queryPage(@RequestBody EmployeeBO bo) {
        return ResultUtil.successResult(employeeService.queryPage(bo));
    }

    @PostMapping("/save")
    public String save(@RequestBody EmployeeBO bo) {
        return ResultUtil.saveResult(employeeService.save(bo));
    }

    @PostMapping("/delete")
    public String delete(@RequestBody EmployeeBO bo) {
        return ResultUtil.deleteResult(employeeService.delete(bo));
    }

    @PostMapping("/queryUserRole")
    public String queryMenuOperator(@RequestBody MenuBO bo) {
        RoleBO roleBO = new RoleBO();
        roleBO.setEmployeeId(bo.getId());
        List<RoleBO> selectedList = this.roleService.queryList(roleBO);

        roleBO = new RoleBO();
        roleBO.setCreateId(Constants.getCurrentUser().getEmployeeId());
        List<RoleBO> allRoleList = this.roleService.queryList(roleBO);
        allRoleList.sort(Comparator.comparing(RoleBO::getName));

        Map<String, Object> result = new HashMap<>();
        result.put("selectedIds", selectedList.stream().map(RoleBO::getId).collect(Collectors.toList()));
        result.put("allRoles", allRoleList);
        return ResultUtil.successResult(result);
    }

    @PostMapping("/saveUserRole")
    public String saveUserRole(@RequestBody EmployeeBO bo) {
        return ResultUtil.successResult(employeeService.saveUserRole(bo));
    }

    @PostMapping("/updatePassword")
    public String updatePassword(@RequestBody EmployeeBO bo) {
        bo.setId(Constants.getCurrentUser().getEmployeeId());
        if (employeeService.updatePassword(bo) > 0) {
            return ResultUtil.successResult("密码修改成功！");
        } else {
            return ResultUtil.failureResult("密码修改失败！");
        }
    }
}
